<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class potongan extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library("phpexcel");
        $this->load->library("PHPExcel/iofactory");

        $this->load->model("report/m_potongan");
        $this->load->model("crud/m_info_usaha");
    }

    function index() {
        $this->rencana();
    }

    function anggota_rencana()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Rencana Potongan Anggota');
        $this->load->view('pengurus/br-08/rencana_potongan');
    }

    function cetak_rencana($id_anggota)
    {
        $this->load->helper('dompdf');
        $this->load->helper('file');
        
        $data["rencana_potongan"] = $this->m_potongan->get_active($id_anggota)->result();
        $data['info_usaha'] = $this->m_info_usaha->get()->result();

        $data['title'] = "Just a test";
        $this->load->view('report/rencana_potongan_anggota', $data);
        //$html = $this->load->view('report/rencana_potongan_anggota', $data, true);
        //pdf_create($html, 'PJM_' . $data['Pinjam']->NAMA_ANGGOTA . '_' . $data['Pinjam']->ID_PINJAMAN);
    }

    function rencana()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Potongan');
        $data["info_usaha"] = $this->m_info_usaha->get()->result();
        $this->load->view('pengurus/br-08/potongan/rencana', $data);
    }

    function hasil()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Potongan');
        $data["info_usaha"] = $this->m_info_usaha->get()->result();
        $this->load->view('pengurus/br-08/potongan/hasil', $data);
    }

    function detail()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Potongan');
        $data["info_usaha"] = $this->m_info_usaha->get()->result();
        $this->load->view('pengurus/br-08/potongan/detail', $data);
    }

    function slip_rencana()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Potongan');
        $data["info_usaha"] = $this->m_info_usaha->get()->result();
        $this->load->view('pengurus/br-08/potongan/slip_rencana', $data);
    }

    function rencana_bulanan()
    {
        $date = $this->input->post("KODE_UNIT");
        
//        $usaha = $this->m_info_usaha->get();
//        $where = Array();
//
//        foreach($usaha->result() as $row)
//        {
//            if($this->input->post("info_".$row->KODE_USAHA) == $row->KODE_USAHA)
//                $where[] = "ID_INFO_USAHA = ".$row->KODE_USAHA;
//        }
//
//        echo implode(" OR ", $where);
//
//        $this->load->view("kosong");
//
//        //var_dump($date);
//        $year = date("Y", strtotime($date));
//        $month = date("m", strtotime($date));
//
////        $this->load->model("crud/m_toko");
//        //$this->m_toko->get($filter[0]);

        $result = $this->m_potongan->sum_potongan();

        
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Rencana Potongan")
                ->setDescription("description");

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);


        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getStyle('A1')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A1'), 'B1:E1' );


//$objPHPExcel->getActiveSheet()->setBreak( 'A10' , PHPExcel_Worksheet::BREAK_ROW );
        // Assign cell values

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "No");
        $objPHPExcel->getActiveSheet()->setCellValue('B1', "ID Anggota");
        $objPHPExcel->getActiveSheet()->setCellValue('C1', "GGI");
        $objPHPExcel->getActiveSheet()->setCellValue('D1', "Nama");
        $objPHPExcel->getActiveSheet()->setCellValue('E1', "Potongan");

        $i = 2;
        foreach($result->result() as $row) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i - 2);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $row->ID_ANGGOTA);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $row->GGI);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $row->NAMA_ANGGOTA);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $row->jumlah);
            $i++;
        }
        $objPHPExcel->getActiveSheet()->setCellValue("E$i", "=SUM(E1:E".($i-1).")");
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&C&HLaporan Rencana Potongan\n!');
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="Rencana Potongan.xls"');
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }
}
?>
